<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Common\ResponseCode;
use App\Models\AdminMenuModel;
use Illuminate\Http\Request;
use Validator;

class MenuController extends ApiController {


    /**
     * @param Request $request
     * 菜单类别
     */
    public function menuList(Request $request)
    {
        //查询所属系统
        $SystemType = !empty($request->SystemType) ? $request->SystemType : 1;
        $menuList = AdminMenuModel::getPermissionTreeList($SystemType);
        return $this->apiResponse($menuList);
    }

    /**
     * 获取编辑页面
     */
    public function getEditInfo(Request $request)
    {
        $rule = [
            'menuId' => 'required|numeric',
        ];
        $messages = [
            'menuId.required' => '菜单ID不能为空',
            'menuId.numeric' => '菜单ID类型错误',
        ];

        $error = Validator::make($request->all(), $rule, $messages)->errors()->first();
        if (!empty($error)) {
            return $this->apiResponse(null, ResponseCode::COMMON_ERROR, $error);
        }

        $menuId = $request->input('menuId');
        $select = ['id', 'systemType', 'menuName', 'parentId', 'icoClass', 'sort', 'status', 'menuType', 'webUrl',  'keyword'];
        $menuInfo = AdminMenuModel::findRecordOneCondition(['id' => $menuId], $select)->load('getMenuInfo');
        return $this->apiResponse($menuInfo);

    }

    /**
     * TODO:添加或者修改
     */
    public function addOrEdit(Request $request)
    {
        $rule = [
            'systemType' => 'required|numeric',
            'parentId' => 'required|numeric',
            'status' => 'required|numeric',
            'menuName' => 'required',
        ];
        $messages = [
            'systemType.required' => '系统类型不能为空',
            'parentId.required' => '父类ID不能为空',
            'status.required' => '状态不能为空',
            'menuName.required' => '菜单名称',
        ];

        $error = Validator::make($request->all(), $rule, $messages)->errors()->first();
        if (!empty($error)) {
            return $this->apiResponse(null, ResponseCode::COMMON_ERROR, $error);
        }

        $menuId = $request->input('menuId');
        $SystemType = $request->input('systemType');
        $ParentId = $request->input('parentId');
        $Sort = $request->input('sort');
        $Status = $request->input('status');
        $WebUrl = $request->input('webUrl');
        $Keyword = $request->input('keyword');
        $MenuType = $request->input('menuType');
        $IcoClass = $request->input('icoClass');
        $userId = $request->post('userId');
        $menuName = $request->post('menuName');
        if ($MenuType == 2) {
            $menuInfo = AdminMenuModel::find($ParentId);
            $Keyword = $menuInfo['Keyword'];
        }
        $data = [
            'systemType' => $SystemType,
            'menuName' => $menuName,
            'parentId' => $ParentId,
            'sort' => !empty($Sort) ? $Sort : 0,
            'status' => $Status,
            'webUrl' => $WebUrl == 1 ? NULL : $WebUrl,
            'keyword' => $Keyword,
            'menuType' => $MenuType,
            'icoClass' => $IcoClass,
        ];
        if ($this->checkName($menuId, $Keyword, $SystemType) == 'true' && $MenuType == 1) {
            return $this->apiResponse(null, ResponseCode::COMMON_ERROR, '关键字已经存在');
        }
        //添加
        if ($menuId == -1) {
            $data['creator'] = $userId;
            $data['createTime'] = date("Y-m-d H:i:s");
            AdminMenuModel::addRecord($data);
            return $this->apiResponse(null, ResponseCode::SUCCESS, '添加成功');
        } else {
            AdminMenuModel::updateRecordORM($menuId, $data);
            return $this->apiResponse(null, ResponseCode::SUCCESS, '修改成功');
        }
    }

    /**
     * @param $MenuId
     * @param $Keyword
     * @return string 验证名称重复
     */
    public function checkName($MenuId, $Keyword, $SystemType)
    {
        $where = [
            'id' => ['!=' => $MenuId],
            'keyword' => $Keyword,
            'menuType' => 1,
            'systemType' => $SystemType,
            'status' => 1,
            'isDelete' => 0,
        ];
        $nameCount = AdminMenuModel::getRecordCountCondition($where);
        if ($nameCount > 0) {
            return 'true';
        } else {
            return 'false';
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取上级菜单
     */
    public function getMenuList(Request $request)
    {
        //查询所属系统
        $SystemType = !empty($request->SystemType) ? $request->SystemType : 1;
        $menuInfo = AdminMenuModel::getRecordListCondition(['systemType' => $SystemType, 'menuType' => 1, 'status' => 1, 'isDelete' => 0], ['id', 'menuName']);
        return $this->apiResponse(['menuInfo' => $menuInfo]);
    }


    /**
     * @param Request $request
     * 删除
     */
    public function menuDel(Request $request)
    {
        $rule = [
            'menuId' => 'required',
        ];
        $messages = [
            'menuId.required' => '菜单Id不能为空',
        ];

        $error = Validator::make($request->all(), $rule, $messages)->errors()->first();
        if (!empty($error)) {
            return $this->apiResponse(null, ResponseCode::COMMON_ERROR, $error);
        }

        $menuId = $request->post('menuId');
        $userId = $request->post('userId');
        $userCount = AdminMenuModel::getRecordCountCondition(['status' => 1, 'isDelete' => 0, 'parentId' => $menuId, 'menuType' => 1]);
        if ($userCount > 0) {
            return $this->apiResponse(null, ResponseCode::COMMON_ERROR, '当前菜单下有子类');
        }
        $data = [
            'isDelete' => 1,
        ];
        AdminMenuModel::updateRecordORM($menuId, $data);
        return $this->apiResponse(null, ResponseCode::SUCCESS, '删除成功');
    }

    /**
     * @param Request $request
     * 更改菜单状态
     */
    public function upStatus(Request $request)
    {
        $menuId = $request->input("menuId");
        $status = $request->input("status");
        $userCount = AdminMenuModel::getRecordCountCondition(['status' => 1, 'isDelete' => 0, 'parentId' => $menuId, 'menuType' => 1]);
        if ($userCount > 0) {
            return $this->apiResponse(null, ResponseCode::COMMON_ERROR, '当前菜单下有子类');
        }
        $data = [
            'status' => $status,
        ];
        AdminMenuModel::updateRecordORM($menuId, $data);
        return $this->apiResponse(null, ResponseCode::SUCCESS, '状态修改成功');

    }
}

